<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

/**
 * Authenticator_Interface
 *
 * @package     AuthenticatedController
 * @subpackage  Authenticator_Interface
 * @author      Doug Everly
 *
 * Defines the interface for Authenticator subclasses.
 *
 */
interface Authenticator_Interface
{
    public function loggedin();
    public function login();
    public function make_session();
}

/**
 * LDAPAuthenticator
 *
 * @package     AuthenticatedController
 * @subpackage  LDAPAuthenticator
 * @author      Doug Everly
 *
 * Base class for Authenticators.
 *
 */
class Authenticator {

    //  Constructor
    function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
    }

    /**
     * loggedIn()
     *
     * Indicates if user is logged in.
     * Returns:
     *   username if logged in
     *   FALSE if not logged in
     *
     */
     
    function loggedIn()
    {
        return $this->CI->session->userdata('username');
    }
    
    /**
     * login()
     *
     * If username and password exists and authenticates, then create session
     * Returns:
     *   1 if login successful
     *   0 if login unsuccessful
     *
     */
     
    function login($username, $password)
    {
        if ($username == $this->CI->config->item('auth_username') && $password == $this->CI->config->item('auth_password'))
        {
            $this->make_session($username, $password, array('test'));
            return 1;
        }
        else
        {
            return 0;
        }
    }

    /**
     * groups()
     *
     * Returns:
     *   Groups
     *
     */
     
	function groups()
	{
        return $this->CI->session->userdata('groups');
	}
    
    /**
     * make_session()
     *
     * Creates a browser session cookie with username, group membership and expiration time.
     * Cookie is encrypted by CodeIgniter if "sess_encrypt_cookie" is TRUE.
     *
     * Returns:
     *   void
     *
     */
     
    function make_session($username, $groups)
    {
        if (!is_array($groups))
        {
            $groups = array($groups);
        }
        $this->CI->session->set_userdata(array(
			'username' => $username,	
			'groups'   => $groups,
			'expires' => time() + $this->CI->config->item('session_duration'))
			);
    }

    
}

